package ICM.client;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.JButton;

import common.CommandMessage;
import common.Request;
import common.TextMessage;
import common.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;



public class MonitoringRequestUI extends JPanel implements ActionListener, Observer {
	private JButton btnOk;
	private JButton btnCancel;
	private ClientUI ui;
	private Client c;
	private  JComboBox comboBox;
	private DefaultTableModel defaultModel = null;
	private Vector newRow = null;
	private JTable table;
	Hashtable<Integer,String> reqList=new Hashtable<Integer,String>(); 
	String[] reqinfo=new String[4]; 
	
	public MonitoringRequestUI(Client c, ClientUI ui) {
		
		this.c=c;
		this.ui=ui;
		c.addObserver(this);
		setLayout(null);
		
		
		CommandMessage msg = new CommandMessage("Get User requests",new Integer[] {Integer.parseInt(Client.Login)});
		// requests.
		try {
			c.sendToServer(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
		    Thread.sleep(150);
		} catch(InterruptedException ex) {
		    Thread.currentThread().interrupt();
		}
		
		
		JLabel lblUserid = new JLabel("Your ID is:      ");
		lblUserid.setBounds(110, 72, 128, 14);
		add(lblUserid);
		
		JLabel lblMonitoringRequest = new JLabel("Monitoring Request");
		lblMonitoringRequest.setBounds(192, 24, 140, 20);
		add(lblMonitoringRequest);
		
		btnOk = new JButton("OK" , new ImageIcon(MonitoringRequestUI.class.getResource("/client/images/check.png")));
		btnOk.setBounds(97, 163, 102, 34);
		add(btnOk);
		btnOk.addActionListener( this);
		
		btnCancel = new JButton("Cancel" , new ImageIcon(MonitoringRequestUI.class.getResource("/client/images/cancel.png")));
		
		btnCancel.setBounds(262, 163, 112, 34);
	
		btnCancel.addActionListener( this);
		add(btnCancel);
		
		JLabel lblYourActiveRequests = new JLabel("Your active requests");
		lblYourActiveRequests.setBounds(110, 111, 120, 14);
		add(lblYourActiveRequests);
		
		//Adding active requests from user to combobox 
		comboBox = new JComboBox();	
		Iterator<Integer> keySetIterator = reqList.keySet().iterator();
		while(keySetIterator.hasNext()){			
			  Integer item = keySetIterator.next();
			  comboBox.addItem(item);
			}

        	
		comboBox.setBounds(240, 107, 102, 22);
		add(comboBox);
		
		JLabel lblNewLabel = new JLabel(Client.Login);
		lblNewLabel.setBounds(192, 72, 80, 14);
		add(lblNewLabel);
		
	}
	
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			
			if (e.getSource().equals(btnOk)) 
			{
				//Reading info from comboBox 							 
			    int reqId = (Integer) comboBox.getSelectedItem();
				//Sending message to DB to find info about specific request    
			    CommandMessage msg = new CommandMessage("Get request stage",new Integer[] {reqId,Integer.parseInt(reqList.get(reqId))});		   
        	    try {
					c.sendToServer(msg);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			    
        	    try {
        		    Thread.sleep(200);
        		} catch(InterruptedException ex) {
        		    Thread.currentThread().interrupt();
        		}
        	   
        	    //Creating new table with data about request
			    table = new JTable();		    
			    table.setModel(new DefaultTableModel(
						new Object[][] {},new String[] {"Request ID", "Stage name", "Stage end date"}
					));
					defaultModel = (DefaultTableModel) table.getModel();
				JScrollPane pane = new JScrollPane(table); 
				pane.setBounds(130, 240, 250, 40);
				defaultModel.addRow(new Object[]{reqinfo[3], reqinfo[2], reqinfo[1]});
				add(pane);
			    
		    }	
			
				if(e.getSource().equals(btnCancel))
					try {
						ui.SetContent(ClientUI.Main_Screen);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}						
	 }
		
		/**
		 * Handle massage from Observable class in our case Client
		 */
		@Override
		public void update(Observable arg0, Object arg1) {
			
			String msg = "";
			if (arg1 instanceof TextMessage)
				msg = ((TextMessage)arg1).ToDebugMessage();
			else if (arg1 instanceof CommandMessage) {
				CommandMessage ms=(CommandMessage)arg1;
				//Searching for user active requests 
				if(ms.getCommand().equals("UserRequests"))				
					reqList = (Hashtable<Integer,String>) ms.getParams()[1];
				else 
					//Searching for info about specific request 
					if(ms.getCommand().equals("RequestInfo"))				
						reqinfo = (String[]) ms.getParams()[1];
			}
			else
				msg = arg1.toString();
		}	
}
